<template>
  <div class="app-container" ref="appcontainer">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="门店名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入门店名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店电话" prop="storePhone">
        <el-input
          v-model="queryParams.storePhone"
          placeholder="请输入门店电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="applyState">
        <el-select v-model="queryParams.applyState" placeholder="请选择审核状态" clearable size="small">
          <el-option
            v-for="dict in applyStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分类" prop="applyState">
        <el-select v-model="queryParams.storeClassify" placeholder="请选择分类" clearable size="small">
          <el-option
            v-for="item in classifyOptions"
            :label="item.classifyName"
            :value="item.id"
          />
        </el-select>
     </el-form-item>
      <el-form-item>
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
        <el-button  size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain

          size="mini"
          @click="passAll"
          :disabled="multiple"
          v-hasPermi="['store:storeApply:edit']"
        >批量通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain

          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['store:storeApply:remove']"
          v-if="false"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain

          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['store:storeApply:export']"
          v-if="false"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :height="tableHeight" :data="storeApplyList" @selection-change="handleSelectionChange">
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="门店名称" align="center" prop="storeName" />
      <el-table-column label="门店电话" align="center" prop="storePhone" />
      <el-table-column label="门店地址" align="center" prop="addressName" />
      <el-table-column label="人均消费" align="center" prop="consumption" />
      <el-table-column label="定位地址" align="center" prop="location" />
      <el-table-column label="省名称" align="center" prop="provinceName" />
      <el-table-column label="市名称" align="center" prop="cityName" />
      <el-table-column label="邀请码" align="center" prop="agentCode" />
      <el-table-column label="审核状态" align="center" prop="applyState">
        <template slot-scope="scope">
          <dict-tag :options="applyStateOptions" :value="scope.row.applyState"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"

            @click="handleUpdate(scope.row)"
            v-hasPermi="['store:storeApply:edit']"
            v-if="scope.row.applyState == 'audit_IN'"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"

            @click="handleUpdate(scope.row)"
            v-hasPermi="['store:storeApply:edit']"
            v-else
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改门店申请对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="门店logo">
              <ImageScan v-model="form.storeImage"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="门店展示">
              <img class="storeShowImage" v-for="item in form.storeShowImage" :key="item":src="item" alt="">
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="店内环境">
              <ImageScan v-model="form.storeBacImage"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="营业执照" prop="businessImage">
          <ImageScan v-model="form.businessImage"/>
        </el-form-item>
        <el-form-item label="门店名称" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入门店名称" readonly/>
        </el-form-item>
        <el-form-item label="门店电话" prop="storePhone">
          <el-input v-model="form.storePhone" placeholder="请输入门店电话" readonly />
        </el-form-item>
        <el-form-item label="门店地址" prop="addressName">
          <el-input v-model="form.addressName" type="textarea" placeholder="请输入内容" readonly/>
        </el-form-item>
        <el-form-item label="定位地址" prop="location" >
          <el-input v-model="form.location" type="textarea" placeholder="请输入内容"readonly />
        </el-form-item>
        <el-form-item label="品牌介绍" prop="brandDetail">
          <el-input v-model="form.brandDetail" type="textarea" placeholder="请输入内容" readonly/>
        </el-form-item>
        <el-form-item label="人均消费" prop="consumption">
          <el-input v-model="form.consumption" placeholder="请输入人均消费" readonly/>
        </el-form-item>
        <el-form-item label="服务保障" prop="serviceItem">
          <el-input v-model="serviceItem" type="textarea" readonly/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="营业开始时间" prop="businessStart">
              <el-input v-model="form.businessStart" placeholder="开始营业时间" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="营业结束时间" prop="businessEnd">
              <el-input v-model="form.businessEnd" placeholder="结束营业时间" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="省名称" prop="provinceName" >
              <el-input v-model="form.provinceName" placeholder="请输入省名称" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="市名称" prop="cityName" >
              <el-input v-model="form.cityName" placeholder="请输入市名称" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="审核状态" prop="applyState">
          <el-select v-model="form.applyState" placeholder="请选择审核状态" disabled readonly>
            <el-option
              v-for="dict in applyStateOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('pass')"  v-if="form.applyState == 'audit_IN'">通 过</el-button>
        <el-button type="warning" @click="showRefuse" v-if="form.applyState == 'audit_IN'">拒 绝</el-button>

        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 拒绝信息对话框 -->
    <el-dialog :close-on-click-modal="false"  title="拒绝原因" :visible.sync="refuseOpen" width="500px" append-to-body>
      <el-form ref="refuseForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="拒绝原因" prop="refuseMsg">
          <el-input v-model="form.refuseMsg" type="textarea" placeholder="请输入拒绝原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" @click="submitForm('refuse')">确 定</el-button>
        <el-button @click="refuseCancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listStoreApply, getStoreApply, logicDelStoreApply, refuse,pass, addStoreApply, updateStoreApply, exportStoreApply } from "@/api/store/storeApply";
import { listStoreclassify } from "@/api/storeclassify/storeclassify";
import Template from "../../protocolTemplate/index";


export default {
  name: "StoreApply",
  components: {Template},
  data() {
    return {
      tableHeight:0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 门店申请表格数据
      storeApplyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核状态字典
      applyStateOptions: [],
      //分类选择
      classifyOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeImage: null,
        storeBacImage: null,
        storeName: null,
        storePhone: null,
        addressName: null,
        brandDetail: null,
        consumption: null,
        serviceItem: null,
        businessStart: null,
        businessEnd: null,
        location: null,
        provinceName: null,
        provinceCode: null,
        cityName: null,
        cityCode: null,
        latitude: null,
        longitude: null,
        geoCode: null,
        applyState: 'audit_IN',
        storeClassify:null
      },
      // 表单参数
      form: {},
      // 拒绝原因对话框
      refuseOpen:false,
      // 表单校验
      rules: {
        refuseMsg: [
          { required: true, message: "拒绝原因不能为空", trigger: "blur" }
        ]
      },
      serviceItem:''
    };
  },
  beforeRouteEnter(to, form, next) {
    next(vm => {
      const applyState = vm.$route.query && vm.$route.query.applyState;
      if(applyState){
        vm.queryParams.applyState = applyState;
        vm.getList();
      }
    });
  },
  created() {
    this.getList();
    this.calHeight()
      this.getDicts("jy_audit_state").then(response => {
      this.applyStateOptions = response.data;
    });
    this.getClassify()
  },
  watch: {
    'showSearch':'calHeight'
  },
  methods: {
    getClassify(){
      let params = {
        parentId: 0,
      }
      listStoreclassify(params).then(res =>{
        this.classifyOptions = res.rows
      })
    },
    calHeight () {
          this.$nextTick(() => {
              const rect = this.$refs.appcontainer.getBoundingClientRect();
              let height= this.$refs.queryForm.$el.offsetHeight;
              this.tableHeight = rect.height-height-120;
          })
      },
    /** 查询门店申请列表 */
    getList() {
      this.loading = true;
      listStoreApply(this.queryParams).then(response => {
        this.storeApplyList = response.rows;
        // this.storeApplyList.forEach(item => {
        //   item.storeShowImage ? item.storeShowImage.split(",") : []
        // })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeImage: null,
        storeBacImage: null,
        businessImage: null,
        storeName: null,
        storePhone: null,
        addressName: null,
        brandDetail: null,
        consumption: null,
        serviceItem: null,
        businessStart: null,
        businessEnd: null,
        location: null,
        provinceName: null,
        provinceCode: null,
        cityName: null,
        cityCode: null,
        latitude: null,
        longitude: null,
        geoCode: null,
        applyState: null,
        delState: null,
        delUser: null,
        delType: null,
        delTime: null,
        createBy: null,
        createType: null,
        createTime: null,
        updateBy: null,
        updateType: null,
        updateTime: null,
        storeShowImage: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.showSearch=false;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStoreApply(id).then(response => {
        this.form = response.data;
        this.form.storeShowImage = this.form.storeShowImage ? this.form.storeShowImage.split(",") : []
        this.open = true;
        let arr = JSON.parse(response.data.serviceItem)
        var str = ''
        for(var i = 0; i < arr.length; i++){
          str = str+arr[i].dictLabel+" "
        }
        this.serviceItem = str
        this.title = "门店申请";
      });
    },
    showRefuse(){
      this.refuseOpen = true
    },
    refuseCancel(){
      this.refuseOpen = false
    },
    passAll(){
      const id = this.ids
      let arr = this.storeApplyList
      for(let item of arr){
        for(let arrId of id){
          if(arrId+'' == item.id+''){
            if(item.applyState == 'audit_PASS'){
              this.msgError("已通过审核的无法再次审核")
              return
            }
          }
        }
      }
      pass(id).then(response => {
        this.msgSuccess("成功");
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm(type) {
      const id = this.form.id || this.ids
      let bool = true;
      if (type == "refuse") {
        this.$refs["refuseForm"].validate(valid => {
          bool = valid;
        });
      }
      if(bool){
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (type == 'refuse') {

              const array = this.form.storeShowImage;
              const string = array.join(',');
              this.form.storeShowImage = string

              refuse(this.form).then(response => {
                this.msgSuccess("成功");
                this.open = false;
                this.refuseOpen = false;
                this.getList();
              });
            } else {
              pass(id).then(response => {
                this.msgSuccess("成功");
                this.open = false;
                this.refuseOpen = false;
                this.getList();
              });
            }
          }
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除门店申请编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicDelStoreApply(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有门店申请数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportStoreApply(queryParams);
      }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style scoped>
.storeShowImage {
  width: 150px;
  height: 150px;
}
</style>
